package cn.itsource.user.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.query.LogininfoQuery;
import cn.itsource.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录账户接口",description = "登录账户接口详细描述")
public class LogininfoController {
    @Autowired
    private ILogininfoService logininfoService;
    /**
     * 查询一个  localhost:8080/logininfo/findone  1
     * localhost:8080/logininfo/1
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "登录账户查找",notes = "根据ID查找登录账户")
    public Logininfo findOne(@PathVariable("id") Long id){
        Logininfo one = logininfoService.findOne(id);
        return one;
    }

    /**
     * 查询所有localhost:8080/logininfo
     * @return
     */
    @GetMapping
    @ApiOperation(value = "登录账户查找",notes = "查找所有登录账户")
    public List<Logininfo> findAll(){
        List<Logininfo> list = logininfoService.findAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/logininfo*/
    @ApiOperation(value = "添加或者修改登录账户",notes = "添加或者修改登录账户")
    public AjaxResult addorUpdate(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId()==null){//添加
                logininfoService.add(logininfo);
            }else{//修改
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/logininfo/4*/
    @ApiOperation(value = "删除登录账户",notes = "根据ID删除登录账户")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            logininfoService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找登录账户",notes = "分页查找登录账户")/*localhost:8080/logininfo*/
    public PageList<Logininfo> queryForPage(@RequestBody LogininfoQuery logininfoQuery){
        PageList<Logininfo> pageList = logininfoService.queryForPage(logininfoQuery);
        return pageList;
    }
}
